/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/
/*
Authors: Ram Ovadia 201374022 and Lior Fass 300069705
Project: HW Assignment 3
Description: This is the implementation of the BaggageTracker main utils module used in the project.  
*/
/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/

#ifndef BT_UTIL_H
#define BT_UTIL_H

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include "List.h"
#include "sorterThread.h"
#include "loaderThread.h"

#define IN_TXT_FILE_ARG 1
#define NUM_OF_SLOTS_ARG 2
#define NUM_OF_SORTERS_ARG 3
#define OUT_TXT_FILE_ARG 4

#define NUM_OF_LOADERS 1

#define EMPTY_SLOT "\0"
#define LINE_LEN 128
#define SLOT_LEN 3

//typedef struct tSlot { char s[3]; } tSlot; //DUMB ME!
typedef char tSlot;

typedef struct Plane
{
	tSlot *dest;
	HANDLE luggage_mutex;
	int luggage_count;
} tPlane;

typedef struct tArgs
{
	FILE *in_f;
	int num_of_slots;
	HANDLE sync_sempahore;
	tSlot *slots;
	HANDLE *slots_mutexes;
	List *planes;
	HANDLE planes_push_mutex;
	HANDLE loader_thread_handle;
} tArgs;

/*
* Checkes weather str is a string describing an integer
*
* Input:
* -----
* str - pointer to a string      
*
* Output:
* -----
* Return Value:
* TRUE - str describes a valid integer.
* FALSE - otherwise.
*/
BOOL isInteger(char *str);

/*
* wraps windows CreateThread function
*
* Input:
* -----
* (*func)( LPVOID* ) - pointer to thread start Function 
* ArgPtr - pointer to arguments to be passed to thread      
*
* Output:
* -----
* Return Value:
* HANDLE to the new thread
*/
HANDLE RunThread(  int (*func)( LPVOID* ) , LPVOID ArgPtr );

/*
* function that creates all the threads, waits for them to finish and call the alphabetical sorter 
*
* Input:
* -----
* common - pointer to the common variables struct 
* num_of_sorters - num of sorter threads to create
*
* Output:
* -----
* Return Value:
* 0 - succeed
* !0 - otherwise
*/
int dispatcher(tArgs *common, int num_of_sorters);

/*
* function that checks the validity of the input argc and argv and opens the input file
*
* Input:
* -----
* argc,argv - input from run line
* 
*
* Output:
* -----
* num_of_slots - number of slots to create after validity check
* num_of_sorters - number of sorters thread to create after validity check
* in_f - pointer to input file handle
* out_f - pointer to input file handle
* Return Value:
* 0 - succeed
* !0 - otherwise
*/
int input_check(int argc, char *argv[], int *num_of_slots, int *num_of_sorters, FILE **in_f, FILE **out_f);

/*
* function that writes the output file summerizing the run
*
* Input:
* -----
* common - common arguments struct
* out_f - pointer to the output file
* 
* Output:
* -----
* Return Value:
* 0 - succeed
* !0 - otherwise
*/
int output_generate(tArgs *common, FILE *out_f);

/*
* compares equality of tSlot slot and the tSlot member of the tPlane struct plane.       
* used to check if a baggage destination and a plane destination match. 
*
* Input:
* -----
* slot - tSlot pointer      
* plane - tPlane struct pointer, whose tSlot member
*
* Output:
* -----
* Return Value:
* 0 - equal.
* !0 - unequal.
*/
int comp_planes(void *slotA, void *slotB);

/*
* allocates and initialize slots sorters mutexes 
*
* Input:
* -----
* num_of_slots - number of slots      
* num_of_sorters - num of sorter threads
* in_f - pointer to file handle, to be inserted to the commmon args struct
*
* Output:
* -----
* Return Value:
* initialized struct of the common arguments
* NULL if fails
*/
tArgs *init(int num_of_slots, int num_of_sorters, FILE* in_f);


/*
* Close handles to files and objects, free memory 
*
* Input:
* -----
* common - pointer to common arguments strtuct      
* out_f - pointer to output file handle
* in_f - pointer to input file handle
* Output:
* -----
* Return Value:
* 0 succeed
* !0 else
*/
int finalize_run(tArgs *common, FILE *out_f, FILE *in_f);


#endif //BT_UTIL_H